﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IO = System.IO;
using System.Windows;
using System.Windows.Threading;
using System.Xml.Serialization;
using System.IO.Compression;
using System.Text.RegularExpressions;

namespace SLP
{
    // not a true extension class but more of a helper class
    public static class XML
    {
        /// <summary>
        /// Generic XML Serializer Save XML File
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="SourceData">Serializable object to be stored</param>
        /// <param name="FileName">FileName to write to</param>
        public static void Save<T>(T SourceData, string FileName)
        {
            IO.Directory.CreateDirectory(IO.Path.GetDirectoryName(FileName));            
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            IO.FileStream TargetStream = new IO.FileStream(FileName, IO.FileMode.Create, IO.FileAccess.Write, IO.FileShare.Read);            
            IO.TextWriter TargetFile = new IO.StreamWriter(TargetStream);
            serializer.Serialize(TargetFile, SourceData);
            TargetFile.Flush();
            TargetFile.Close();
            TargetStream.Close();
        }

        /// <summary>
        /// Generic XML Serializer Load XML File
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="FileName">Filename to read from</param>
        /// <returns>Data object of type T</returns>
        public static dynamic Load<T>(string FileName)
        {
            IO.Stream SourceData = null;
            object Data = new object();
            if (IO.File.Exists(FileName))
            {
                SourceData = new IO.FileStream(FileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.ReadWrite);
                XmlSerializer deserializer = new XmlSerializer(typeof(T));
                IO.TextReader SourceFile = new IO.StreamReader(SourceData);
                Data = deserializer.Deserialize(SourceFile);
                SourceFile.Close();
            }
            return (T)Data;
        }

        /// <summary>
        /// Generic XML Serializer Save Compressed XML File
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="SourceData">Serializable object to be stored</param>
        /// <param name="FileName">FileName to write to</param>
        public static void SaveCompressed<T>(T SourceData, string FileName)
        {
            IO.Directory.CreateDirectory(IO.Path.GetDirectoryName(FileName));
            IO.Stream TargetData = new IO.MemoryStream();
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            IO.FileStream TargetFile = new IO.FileStream(FileName, IO.FileMode.Create, IO.FileAccess.Write, IO.FileShare.Read);
            TargetData = new GZipStream(TargetFile, CompressionMode.Compress);
            IO.TextWriter writer = new IO.StreamWriter(TargetData);
            serializer.Serialize(writer, SourceData);
            writer.Flush();
            writer.Close();
            TargetFile.Close();
        }

        /// <summary>
        /// Generic XML Serializer Load Compressed XML File
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="FileName">Filename to read from</param>
        /// <returns>Data object of type T</returns>
        public static dynamic LoadCompressed<T>(string FileName)
        {
            IO.Stream SourceData = null;
            object Data = new object();
            if (IO.File.Exists(FileName))
            {
                SourceData = new GZipStream(
                    new IO.FileStream(FileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.ReadWrite),
                    CompressionMode.Decompress);
                XmlSerializer deserializer = new XmlSerializer(typeof(T));
                IO.TextReader reader = new IO.StreamReader(SourceData);
                Data = deserializer.Deserialize(reader);
                reader.Close();
            }
            return (T)Data;
        }
    }
}
